package com.sbm.merge.设计模式.decorator;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class MessageManagerFactory implements ApplicationContextAware {

    private static  Map<String, BasicMessgeSenderDecorator> DECORATOR_COMBO_MAP;

    private ApplicationContext applicationContext;

    public static String getComboMapKey(List<String> comboNameList) {
        return comboNameList.stream().sorted().collect(Collectors.joining("|"));
    }

    static {
        DECORATOR_COMBO_MAP =new HashMap<>();
    }


  @PostConstruct
    public void init() {
        Map<String, BasicMessgeSenderDecorator> decoratorMap = applicationContext.getBeansOfType(BasicMessgeSenderDecorator.class);
        decoratorMap.values().forEach((decorator) -> {
           DECORATOR_COMBO_MAP.put(decorator.getCombinationType(), decorator);
        });
      System.out.println(JSON.toJSONString(DECORATOR_COMBO_MAP));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
